package com.ruoyi.mall.product.domain.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mall.product.domain.repository.PmsCategoryBrandRelationMapper;
import com.ruoyi.mall.product.domain.service.IPmsBrandService;
import com.ruoyi.mall.product.service.PmsBrandCategoryRelationAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.product.domain.repository.PmsBrandMapper;
import com.ruoyi.mall.product.domain.entity.PmsBrand;

/**
 * 品牌Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@Service
public class PmsBrandServiceImpl implements IPmsBrandService {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;
    @Autowired
    private PmsBrandCategoryRelationAppService pmsBrandCategoryRelationAppService;

    /**
     * 查询品牌
     *
     * @param id 品牌主键
     * @return 品牌
     */
    @Override
    public PmsBrand selectPmsBrandById(Long id) {
        return pmsBrandMapper.selectPmsBrandById(id);
    }

    /**
     * 查询品牌列表
     *
     * @param pmsBrand 品牌
     * @return 品牌
     */
    @Override
    public List<PmsBrand> selectPmsBrandList(PmsBrand pmsBrand) {
        String key = pmsBrand.getKey();
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            //模糊检索
            wrapper.eq("id", key).or().like("name", key);
            return pmsBrandMapper.selectList(wrapper);

        } else {
            return pmsBrandMapper.selectPmsBrandList(pmsBrand);
        }
    }

    /**
     * 新增品牌
     *
     * @param pmsBrand 品牌
     * @return 结果
     */
    @Override
    public int insertPmsBrand(PmsBrand pmsBrand) {
        return pmsBrandMapper.insertPmsBrand(pmsBrand);
    }

    /**
     * 修改品牌
     *
     * @param pmsBrand 品牌
     * @return 结果
     */
    @Override
    public int updatePmsBrand(PmsBrand pmsBrand) {
        int i = pmsBrandMapper.updatePmsBrand(pmsBrand);
        if (!StringUtils.isEmpty(pmsBrand.getName())) {
            //同步更新品牌表
            pmsBrandCategoryRelationAppService.updateByBrandName(pmsBrand.getId(), pmsBrand.getName());
        }
        return i;
    }

    /**
     * 批量删除品牌
     *
     * @param ids 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deletePmsBrandByIds(Long[] ids) {
        return pmsBrandMapper.deletePmsBrandByIds(ids);
    }

    /**
     * 删除品牌信息
     *
     * @param id 品牌主键
     * @return 结果
     */
    @Override
    public int deletePmsBrandById(Long id) {
        return pmsBrandMapper.deletePmsBrandById(id);
    }

    /**
     * 根据id修改状态
     *
     * @return
     */
    @Override
    public int updatePmsBrandStatus(PmsBrand pmsBrand) {
        return pmsBrandMapper.updateById(pmsBrand);
    }
}
